package me.alide.modules.survey.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import me.alide.common.exception.RRException;
import me.alide.common.utils.R;
import me.alide.modules.survey.entity.OrgInfoEntity;
import me.alide.modules.survey.service.OrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static me.alide.common.utils.Constant.DATA;

/**
 * @author: Anzhen
 * @description:
 * @PROJECT_NAME: alide
 * @create: 2021-07-25 14:31
 **/
@Slf4j
@RestController
@RequestMapping("/org")
public class OrgInfoController {
    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * 增
     * @param orgInfoEntity
     * @return
     */
    @RequestMapping("save")
    public R save(@RequestBody OrgInfoEntity orgInfoEntity) {
        try {
            int save = orgInfoService.save(orgInfoEntity);
            if (save == 1) {
                log.info("保存成功");
            } else {
                throw new RRException("保存失败", 500);
            }
        } catch (Exception e) {
            log.error("保存失败", e);
            return R.error(500, e.getMessage());
        }
        return R.ok();
    }

    /**
     * 删
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        try {
            int deleteCount = orgInfoService.delete(ids);
            if (deleteCount == 1) {
                log.info("删除成功{}条", deleteCount);
            } else {
                throw new RRException("删除失败", 500);
            }
        } catch (Exception e) {
            log.error("删除失败", e);
            return R.error(500, e.getMessage());
        }
        return R.ok();
    }

    /**
     * 改
     */
    @RequestMapping("update")
    public R update(@RequestBody OrgInfoEntity orgInfoEntity) {
        try {
            int save = orgInfoService.update(orgInfoEntity);
            if (save == 1) {
                log.info("更新成功");
            } else {
                throw new RRException("更新失败", 500);
            }
        } catch (Exception e) {
            log.error("更新成功", e);
            return R.error(500, e.getMessage());
        }
        return R.ok();
    }

    /**
     * 查
     */
    @RequestMapping("queryAll")
    public R queryAll() {
        try {
            List<OrgInfoEntity> list = orgInfoService.queryAll();
            return R.ok().put(DATA, list);
        } catch (Exception e) {
            log.error("查询失败", e);
            return R.error();
        }
    }

    /**
     * 查
     */
    @RequestMapping("queryById")
    public R queryById(@RequestParam Long id) {
        try {
            return R.ok().put(DATA, orgInfoService.queryById(id));
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    /**
     * 查
     */
    @RequestMapping("queryByUserId")
    public R queryByUserId(@RequestParam Long userId) {
        try {
            return R.ok().put(DATA, orgInfoService.queryByUserId(userId));
        } catch (Exception e) {
            log.error("查询失败", e);
            return R.error();
        }
    }

    @RequestMapping("search")
    public R queryList(String keyword, int page, int pageSize) {
        IPage<OrgInfoEntity> orgInfoEntities = orgInfoService.search(keyword, page, pageSize);
       return R.ok().put(DATA, orgInfoEntities.getRecords());
    }
}
